package com.sca.bussiness.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @author liuxianfei
 * @since 2020/11/20 13:31
 */
@Component
public class ApplicationContextHelper implements ApplicationContextAware {

    public static ApplicationContext context;

    public ApplicationContextHelper() {
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        ApplicationContextHelper.context = applicationContext;
    }

    public static Object getBean(String beanName) {
        return context != null ? context.getBean(beanName) : null;
    }

    public static <T> T getBean(Class<T> requiredType) {
        return context != null ? context.getBean(requiredType) : null;
    }

}
